<?php

namespace app\model;

use app\utils\CamlHump;
use think\facade\Db;
use think\Model;

class SysDictionary extends BaseModel
{
    protected $convertNameToCamel = true;
    protected $name = 'sys_dictionary';
    protected static $tableTable = 'sys_dictionary';

     // 设置字段信息
      static   $tableKeys = [
        'id'          => 'int',
        'name'        => 'string',
        'code'      => 'string',
        'class_id'       => 'int',
        'class_id_path' => 'string',
        'state' => 'int',
        'order_num' => 'int',
        'remarks' => 'string',
        'info_level' => 'int'
    ];



    static function  build($data)
    {
        $emptyData = [];
        foreach (self::$tableKeys as $key => $value){
            $emptyData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)] ?? '';
        }





        return $emptyData;
    }
    static function  buildForUpdate($data)
    {
        $updateData = [];

        foreach (self::$tableKeys as $key => $value){
            if (isset($data[CamlHump::toCamelCase($key)])) {
                $updateData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)];
            }
        }
        
     





        return $updateData;
    }
    static function createQuery($queryData)
    {
        $query = Db::table('sys_dictionary');

        foreach (self::$tableKeys as $key => $value){
            if (isset($queryData[CamlHump::toCamelCase($key)])) {
                $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
            }
         
        }
        $query->where('del_state', 1);



        return $query;
    }


    static function createQueryById(int $id)
    {
        $query = Db::table('sys_dictionary');
        if (isset($id)) {
            $query->where('id', $id);
        }

        return $query;
    }

    static function addPlus($data)
    {
        $sysAccountAdd = SysDictionary::build($data);
        $result = SysDictionary::create($sysAccountAdd);
        return $result;
    }
    static function updatePlus($data, $id)
    {
        $result = SysDictionary::update($data, ['id' => $id]);
        return $result;
    }
    static function deletePlus($id)
    {
        $result = SysDictionary::destroy($id);
        return $result;
    }
    static function findOneByCode($code)
    {
        $query = Db::table(static::$tableTable);
        return CamlHump::toCamlObj($query->where('code', $code)->find());
    }
}
